package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.link.view.adapter.PickFilterWindowAdapter;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

/*
 * 二级筛选
 * 适用三级联动数据列表和二级联动数据列表
 * 仅适用单选模式
 * 初始位置内容自定义设置
 * 勾选模式可通过方法设置
 * 返回数据为PickResultEntity 封装每层所选数据
 * 重置按钮恢复至初始状态并且回调重置方法返给用户处理(自动关闭window)
 * 待完善
 * */
public class TwoLevelFilterWindow extends BasePopWindow {

    private PickFilterWindowAdapter pickAdapter1;
    private PickFilterWindowAdapter pickAdapter2;
    private RecyclerView recycler1;
    private RecyclerView recycler2;
    private PickResultEntity pickResultEntity1;
    private PickResultEntity pickResultEntity2;

    private String all = "全部"; //初始位置显示内容---全部/不限

    private List<PickFilterEntity<List<PickFilterEntity>>> list;
    private int currentPosition = 0;

    public TwoLevelFilterWindow(Context context) {
        super(context);
    }

    public TwoLevelFilterWindow(Context context, String all) {
        super(context);
        this.all = all;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_filter_packers_2;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recycler1 = holder.getPop().findViewById(R.id.rlv_list);
        recycler2 = holder.getPop().findViewById(R.id.rlv2_list);
        TextView reset = holder.getPop().findViewById(R.id.reset);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);

        //重置
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickResultEntity1.clear();
                pickResultEntity2.clear();
                pickAdapter1.setDefItem(0);
                pickAdapter2.setDefItem(0);
                recycler1.scrollToPosition(0);
                recycler2.setVisibility(View.GONE);
                if (onResultClick != null) {
                    onResultClick.reset();
                }
                dissMiss();
            }
        });

        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(pickResultEntity1, pickResultEntity2);
                    dissMiss();
                }
            }
        });

        if (pickAdapter1 == null) {
            pickAdapter1 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window, null);
            pickAdapter1.setIsDisplay(false); //勾选隐藏
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new PickFilterWindowAdapter(mContext, R.layout.item_pick_filter_child_window_1, null);
            pickAdapter2.setIsDisplay(true); //勾选显示
            pickAdapter2.setItemSelect(true);
            pickAdapter2.setDefItem(0);
        }

        XLinearLayoutManager manager = new XLinearLayoutManager(mContext);
        recycler1.setLayoutManager(manager);
        recycler1.setAdapter(pickAdapter1);
        XLinearLayoutManager manager1 = new XLinearLayoutManager(mContext);
        recycler2.setLayoutManager(manager1);
        recycler2.setAdapter(pickAdapter2);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                pickResultEntity1.setNo(entity.getNo());
                pickResultEntity1.setName(entity.getName());
                pickResultEntity2.setNo(list.get(position).getChildren().get(0).getNo());
                pickResultEntity2.setName(list.get(position).getChildren().get(0).getName());
                if (position == 0) {
                    recycler2.setVisibility(View.GONE);
                    return;
                }
                setData2(list.get(position).getChildren());
                recycler2.setVisibility(View.VISIBLE);
            }
        });

        pickAdapter2.setOnItemClick(new AdapterOnItemClick<PickFilterEntity>() {
            @Override
            public void onItemClick(PickFilterEntity entity, int position) {
                pickResultEntity2.setNo(entity.getNo());
                pickResultEntity2.setName(entity.getName());
            }
        });

        pickResultEntity1 = new PickResultEntity();
        pickResultEntity2 = new PickResultEntity();
    }

    /*
     * 三级联动数据(初始位置内容填充)
     * */
    public void setThreeLevelData(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data, int index) {
        List<PickFilterEntity<List<PickFilterEntity>>> entityList = new ArrayList<>();
        PickFilterEntity<List<PickFilterEntity>> pickFilterEntity = new PickFilterEntity<>();
        List<PickFilterEntity> pickFilterEntityList = new ArrayList<>();
        PickFilterEntity entity = new PickFilterEntity();
        entity.setNo("");
        entity.setName(all);
        pickFilterEntityList.add(entity);
        pickFilterEntity.setNo("");
        pickFilterEntity.setName(all);
        pickFilterEntity.setChildren(pickFilterEntityList);
        entityList.add(pickFilterEntity);
        for (int i = 0; i < data.size(); i++) {
            List<PickFilterEntity<List<PickFilterEntity>>> list = data.get(i).getChildren();
            if (list != null) {
                for (int k = 0; k < list.size(); k++) {
                    if (list.get(k).getChildren() == null) {
                        list.get(k).setChildren(pickFilterEntityList);
                    } else {
                        list.get(k).getChildren().add(0, entity);
                    }
                }
                list.add(0, pickFilterEntity);
            } else {
                data.get(i).setChildren(entityList);
            }
        }
        setData(data.get(index).getChildren());
    }

    /*
     * 二级联动数据(初始位置内容填充)
     * */
    public void setTwoLevelData(List<PickFilterEntity<List<PickFilterEntity>>> data) {
        PickFilterEntity<List<PickFilterEntity>> pickFilterEntity = new PickFilterEntity<>();
        List<PickFilterEntity> pickFilterEntityList = new ArrayList<>();
        PickFilterEntity entity = new PickFilterEntity();
        entity.setNo("");
        entity.setName(all);
        pickFilterEntityList.add(entity);
        pickFilterEntity.setNo("");
        pickFilterEntity.setName(all);
        pickFilterEntity.setChildren(pickFilterEntityList);
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getChildren() == null) {
                data.get(i).setChildren(pickFilterEntityList);
            } else {
                data.get(i).getChildren().add(0, entity);
            }
        }
        data.add(0, pickFilterEntity);
        setData(data);
    }

    private void setData(List<PickFilterEntity<List<PickFilterEntity>>> list) {
        this.list = list;
        List<PickFilterEntity> pickList = new ArrayList<>();
        for (PickFilterEntity<List<PickFilterEntity>> pickFilterEntity : list) {
            PickFilterEntity entity = new PickFilterEntity();
            entity.setNo(pickFilterEntity.getNo());
            entity.setName(pickFilterEntity.getName());
            pickList.add(entity);
        }
        pickResultEntity1.setNo(list.get(currentPosition).getNo());
        pickResultEntity1.setName(list.get(currentPosition).getName());
        pickResultEntity2.setNo(list.get(currentPosition).getChildren().get(0).getNo());
        pickResultEntity2.setName(list.get(currentPosition).getChildren().get(0).getName());
        pickAdapter1.setDatas(pickList);
        pickAdapter1.setDefItem(currentPosition);
        recycler1.setVisibility(View.VISIBLE);
        recycler2.setVisibility(View.GONE);
        recycler1.scrollToPosition(currentPosition);
        if (currentPosition == 0) return;
        setData2(list.get(currentPosition).getChildren());
        recycler2.setVisibility(View.VISIBLE);
    }

    private void setData2(List<PickFilterEntity> list) {
        pickAdapter2.setDatas(list);
        pickAdapter2.setDefItem(0);
    }

    public void setDefaultItem(int index) {
        this.currentPosition = index;
        if (list == null) return;
        setData(list);
    }

    private OnResultClick onResultClick;

    public interface OnResultClick {

        void result(Object key1, Object key2);

        void reset();
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }

}
